using System.Diagnostics;
using _ReplaceString_.UI.Animations;
using _ReplaceString_.UI.Styles;
using RSCore;

namespace _ReplaceString_.UI.Elements;

public class ModElementGroup : UIBase
{
	public ModElementGroup()
	{
		_mods ??= ModHelper.GetModInfo();
		AddStyle(UISize.Fill, UISize.FlexibleHeight, UILayout.Vertical);

		foreach (var mod in _mods)
		{
			var element = new ModElement(mod);
			element.AddStyle(UIRender.Click);
			element.OnLeftClick += ModElement_OnLeftClick;
			Append(element);
		}
	}

	public void ApplyFilter(string filter)
	{
		Debug.Assert(_mods != null);
		RemoveAllChildren();
		foreach (var mod in _mods)
		{
			if (string.IsNullOrEmpty(filter) || mod.Name.SmartMatch(filter) || mod.DisplayName.SmartMatch(filter))
			{
				var element = new ModElement(mod);
				element.AddStyle(UIRender.Click);
				element.OnLeftClick += ModElement_OnLeftClick;
				Append(element);
				element.Activate();
			}
		}
		Recalculate();
	}

	public override void OnDeactivate()
	{
		_leftPanel = null;
		_rightPanel = null;
	}

	public override void OnInitialize()
	{
		base.OnInitialize();

		// 向上传递Height
		var height = Height;
		var parent = Parent;
		height.Pixels += parent.PaddingTop + parent.PaddingBottom;
		parent.Height = height;

		parent = parent.Parent;
		height.Pixels += parent.PaddingTop + parent.PaddingBottom;
		parent.Height = height;

		parent = parent.Parent;
		height.Pixels += parent.PaddingTop + parent.PaddingBottom;
		parent.Height = height;
		parent.Recalculate();
	}

	private static CalculatedStyle _dimensionCache = ModHelper.GetConfigListDimension().Fix();

	private static List<ModInfo>? _mods;

	private LeftPanel? _leftPanel;

	private RightPanel? _rightPanel;

	private ModElement? _selected;

	private void ModElement_OnLeftClick(UIMouseEvent evt, UIElement listeningElement)
	{
		if (_selected == listeningElement)
		{
			_selected.Tag &= ~UITag.Selected;
			Debug.Assert(_rightPanel != null && _leftPanel != null, "Panel应该与Select一致");
			UIMove.FlyOut(Direction.Right).Play(_rightPanel);
			UIMove.FlyOut(Direction.Left).Play(_leftPanel);
			_selected = null;
			return;
		}

		var modElement = (ModElement)listeningElement;
		var top = TopGroup.Create(this);
		var dim = _dimensionCache;
		const int margin = 20;

		if (_selected != null)
		{
			_selected.Tag &= ~UITag.Selected;
		}

		var size = new Vector2((int)(Main.screenWidth - dim.Width) / 2 - margin * 2, dim.Height);

		// Left
		_leftPanel?.Remove();
		UIBase panel = _leftPanel = new LeftPanel(size);
		top.Append(panel);
		panel.Activate();

		var target = new Vector2(margin, dim.Y);
		if (_selected is not null)
		{
			panel.Left.Pixels = target.X;
			panel.Top.Pixels = target.Y;
		}
		else
		{
			UIMove.FlyIn(target, Direction.Right).Play(panel);
		}

		// Right
		_rightPanel?.Remove();
		panel = _rightPanel = new RightPanel(modElement.Mod, size, _leftPanel);
		top.Append(panel);
		panel.Activate();

		target = new Vector2(dim.X + dim.Width + margin, dim.Y);
		if (_selected is not null)
		{
			panel.Left.Pixels = target.X;
			panel.Top.Pixels = target.Y;
		}
		else
		{
			UIMove.FlyIn(target, Direction.Left).Play(panel);
		}

		_selected = modElement;
	}
}